from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from tortoise.contrib.fastapi import register_tortoise
from settings import TORTOISE_ORM
import uvicorn
from apps.routers.Course.urls import course
from apps.routers.UsersManger.urls import users
from apps.routers.TeacherCourse.urls import teacher
from fastapi.staticfiles import StaticFiles
from apps.routers.StudentCourse.urls import student
from apps.routers.User.urls import user
from apps.routers.Question.urls import question
app = FastAPI()


# 添加静态文件服务（在创建FastAPI app之后）用来显示照片
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 只使用Tortoise-ORM注册
register_tortoise(
    app,
    config=TORTOISE_ORM,
    generate_schemas=True,
    add_exception_handlers=True,
)


app.include_router(users,  prefix="/api",  tags=["users"])
app.include_router(course,  prefix="/api",  tags=["course"])
app.include_router(teacher, prefix="/api", tags=["teacher"])
app.include_router(student, prefix="/api", tags=["student"])
app.include_router(user, prefix="/api", tags=["user"])
app.include_router(question, prefix="/api", tags=["question"])

if __name__ == '__main__':
    uvicorn.run("main:app", host='127.0.0.1', port=8000, reload=True)
